package pers.liu.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.liu.entity.Department;
import pers.liu.service.DepartmentService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiuGuangShuo
 * @since 2024年11月13日
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    /**
     * 分页
     */
    @GetMapping
    public PageInfo<Department> list(
            @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
            @RequestParam(value = "pageName",defaultValue = "",required = false) String deptName

    ){
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Department> qw = new QueryWrapper<>();
        //条件匹配
        if(StringUtils.isNotBlank(deptName)){
            //模糊匹配
            qw.like("dept_name",deptName);
        }
        List<Department> list = departmentService.list(qw);
        return new PageInfo<>(list);
    }

    /**
     * ID查询
     */
    @GetMapping("/{depId}")
    public Department byId(@PathVariable Long depId){
        return departmentService.getById(depId);
    }

    /**
     * 查询所有
     */
    @GetMapping("queryAll")
    public List<Department> queryAll(){
        return departmentService.list();
    }

    /**
     * 增加
     */
    @PostMapping
    public boolean save(@RequestBody Department department){
        return departmentService.save(department);
    }

    /**
     * 修改
     */
    @PutMapping
    public boolean update(@RequestBody Department department){
        return departmentService.updateById(department);
    }
    /**
     * 删除
     */
    @DeleteMapping("/{deptId}")
    public boolean delete(@PathVariable Long deptId){
        return departmentService.removeById(deptId);
    }
}
